package com.paradogs.demo.logic.misc;

import com.paradogs.demo.logic.event.PlayerLoginEvent;
import com.paradogs.demo.logic.event.PlayerLogoutEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yumi
 * @date: 2023/11/29  10:34
 * @Description: TODO
 */
@Slf4j
@Component
public class PlayerOnlineHolder {

    /**
     * 在线玩家 uid: template
     */
    private Map<Long, PlayerTemplate> onlinePlayerMap = new HashMap<>();


    public PlayerTemplate getPlayer(Long uid) {
        return onlinePlayerMap.get(uid);
    }

    @EventListener(PlayerLoginEvent.class)
    public void PlayerLoginEventHandler(PlayerLoginEvent event) {
        onlinePlayerMap.put(event.getUid(), event.getPlayerTemplate());
    }

    @EventListener(PlayerLogoutEvent.class)
    public void PlayerLogoutEventHandler(PlayerLogoutEvent event) {
        onlinePlayerMap.remove(event.getUid());
    }

}
